<?php

namespace App\Controllers\Admin;

use Page;
use Input, Notification, Redirect, Sentry, Str;

use App\Services\Validators\PageValidator;

class PagesController extends \BaseController {

    public function index()
    {

        return Redirect::route('admin.customers.index', array('type' => '5'));
    }

    public function show($id)
    {
        $METHOD = 'pages';
        $ACTION = 'index';
        return \View::make('admin.pages.show',compact('METHOD','ACTION'))->with('page', Page::find($id))->withAuthor(Sentry::findUserById(Page::find($id)->user_id)->username);
    }

    public function create()
    {
        $METHOD = 'pages';
        $ACTION = 'index';
        return \View::make('admin.pages.create',compact('METHOD','ACTION'));
    }

    public function store()
    {
        require app_path().'/lib/qiniu/rs.php';
        require app_path().'/lib/qiniu/io.php';
        $name = $_FILES['cover']["name"];
        $filename=explode(".",$name);
        $filename[0]=$this->random(10); //设置随机数长度
        $uploadfile=implode(".",$filename);
        $tmp = $_FILES['cover']["tmp_name"];
        $result = $this->upPicTo7niu($tmp,'suit',$uploadfile,'aebo');
        $page          = new Page;
        $page->title   = Input::get('title');
        $page->body    = Input::get('editorValue');
        $page->point    = Input::get('point');
        $page->cover    = $result['key'];
        $page->dec    = Input::get('dec');
        $page->user_id = Sentry::getUser()->id;
        $page->save();

        Notification::success('新增页面成功！');


    }

    public function edit($id)
    {
        $METHOD = 'pages';
        $ACTION = 'index';
        return \View::make('admin.pages.edit',compact('METHOD','ACTION'))->with('page', Page::find($id));
    }

    public function update($id)
    {

        $page  = Page::find($id);
        if($_FILES['cover']["error"] == UPLOAD_ERR_OK){
            require app_path().'/lib/qiniu/rs.php';
            require app_path().'/lib/qiniu/io.php';
            $name = $_FILES['cover']["name"];
            $filename=explode(".",$name);
            $filename[0]=$this->random(10); //设置随机数长度
            $uploadfile=implode(".",$filename);
            $tmp = $_FILES['cover']["tmp_name"];
            $result = $this->upPicTo7niu($tmp,'pages',$uploadfile,'aebo');
            $page->cover    = $result['key'];
        }
        $page->title   = Input::get('title');
        $page->body    = Input::get('editorValue');
        $page->point    = Input::get('point');
        $page->dec    = Input::get('dec');
        $page->user_id = Sentry::getUser()->id;
        $page->save();

        Notification::success('新增页面成功！');
    }

    function random($length)
    {
        $hash = 'CR-';
        $chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz';
        $max = strlen($chars) - 1;
        mt_srand((double)microtime() * 1000000);
        for($i = 0; $i < $length; $i++)
        {
            $hash .= $chars[mt_rand(0, $max)];
        }
        return $hash;
    }

    public function destroy($id)
    {
        $page = Page::find($id);
        $page->delete();

        Notification::success('删除成功');
        return Redirect::route('admin.pages.index');
    }

    /**
     *上传图片至七牛云
     *$path 图片地址
     *$dirname 在七牛云上的文类名称
     *$filename 文件保存名称
     *$bucket 你的七牛云域名称
     */
    function upPicTo7niu($path,$dirname,$filename,$bucket){
        $contents = @file_get_contents($path);

        $storename = $dirname."/".$filename;
        $bu = $bucket.":".$storename;
        $accessKey = 'FWdy0abUDmanFns5ZArKEHjRl2e5hXeZVsLKSU7M';
        $secretKey = 'zuHKtuuL888TuuavvzbyWnGnytjW33zI1mrAql12';

        Qiniu_SetKeys($accessKey, $secretKey);
        $putPolicy = new \Qiniu_RS_PutPolicy($bu);
        $upToken = $putPolicy->Token(null);
        $putExtra = new \Qiniu_PutExtra();
        $putExtra->Crc32 = 1;
        list($ret, $err) = Qiniu_Put($upToken, $storename, $contents, $putExtra);
        return $ret;
    }

}